import qs from 'qs'

/**
 * 监听页码发生变化
 * @returns
 */
export function useWatchRoute() {
  const router = useRouter()
  const page = ref(1)
  const size = ref(10)
  watch(
    () => router?.currentRoute?.value.hash,
    (hash) => {
      if (!hash) return
      const hashData: any = qs.parse(hash.replace('#', '')) || {}
      page.value = hashData.page ? parseInt(hashData.page) : 1
      size.value = hashData.size ? parseInt(hashData.size) : 10
    },
    {
      immediate: true
    }
  )

  return {
    page,
    size
  }
}
